package com.central.apps.handler.project;

import com.central.app.common.base.BaseHandler;
import com.central.app.service.HandlerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 处理项目管理基础配置业务逻辑
 *
 * @author lidachao
 * @date 2023/5/6
 */
@Component
@Slf4j
public class ProjectConfigHandler extends BaseHandler {

    @Autowired
    private HandlerService handlerService;

    @Override
    public String validateSave(String app, String objName, Map<String, Object> data) throws Exception {
        if ("gq_promilestonecfg".equals(objName)) {
            Integer id = MapUtils.getInteger(data, "id");
            Integer sortNo = MapUtils.getInteger(data, "sortno");

            StringBuilder sql = new StringBuilder();
            sql.append("select id from gq_promilestonecfg where sortno = ").append(sortNo);

            if (id != null) {
                sql.append(" and id != ").append(id);
            }

            List<Map<String, Object>> list = handlerService.findBySql(sql.toString());

            if (CollectionUtils.isNotEmpty(list)) {
                return "序号重复, 请重新设置!";
            }
        }
        return null;
    }
}
